package cn.yotta.user.web.autoconfiguration.context.user;

import cn.yotta.user.web.autoconfiguration.provider.LoginUser;

/**
 * @author wyaoyao
 * @date 2022/11/8 11:02
 *
 */
public class UserContext {

    private final static UserContextStrategy USER_CONTEXT_STRATEGY;

    private static final LoginUser EMPTY_LOGIN_USER = new LoginUser();

    static {
        USER_CONTEXT_STRATEGY = new ThreadLocalUserContextStrategy();
    }

    public static void clear() {
        USER_CONTEXT_STRATEGY.clear();
    }

    public static LoginUser get() {
        return USER_CONTEXT_STRATEGY.get();
    }

    public static Long getUserId() {
        LoginUser user = null == USER_CONTEXT_STRATEGY.get() ? EMPTY_LOGIN_USER : USER_CONTEXT_STRATEGY.get();
        return user.getId();
    }

    public static String getUsername() {
        LoginUser user = null == USER_CONTEXT_STRATEGY.get() ? EMPTY_LOGIN_USER : USER_CONTEXT_STRATEGY.get();
        return user.getUsername();
    }

    public static void set(LoginUser user) {
        USER_CONTEXT_STRATEGY.set(user);
    }

}
